Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed 'wind_2min_timestamp' variable type from character to date-ti… #52

Merged
merged 7 commits into from
Aug 4, 2023

Conversation

jeremylweiss
Copy link
Member

…me when downloading hourly and daily data.

Changed date-time assignment to 'date_datetime' variable in hourly data. Previously was UTC, now MST

…me when downloading hourly and daily data.

Changed date-time assignment to 'date_datetime' variable in hourly data. Previously was UTC, now MST
@jeremylweiss jeremylweiss requested a review from Aariq July 25, 2023 22:18
Copy link
Member

@Aariq Aariq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't tested this out, but looks like it's going to do the trick. Remember to add a bullet point to the NEWS.md file about these changes

R/az_hourly.R Outdated
Comment on lines 94 to 95
dplyr::mutate(date_datetime = lubridate::force_tz(lubridate::ymd_hms(.data$date_datetime), tzone = "America/Phoenix")) %>%
dplyr::mutate(wind_2min_timestamp = lubridate::with_tz(lubridate::parse_date_time(.data$wind_2min_timestamp, orders = "ymdHMSz"), tzone = "America/Phoenix")) %>%
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between force_tz() and with_tz()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that both handle date_datetime and wind_2min_timestamp without any warning message while assigning the correct time zone to each variable.

With date_datetime, there is no time zone assigned to values in the database. Using force_tz directly assigns tzone = "America/Phoenix", while keeping dates and hours correct. I tested with_tz here, too, but it assigned UTC as the time zone and the corresponding dates and hours, which is wrong.

With wind_2min_timestamp, there is a time zone already assigned to values in the database. Using with_tz and tzone = "America/Phoenix" correctly assigns the desired time zone while keeping corresponding dates and hours correct.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NEWS.md file updated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you commit and push the NEWS.md changes? I don't see them in this pull request

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, they're there now. But, I managed to make a conflict

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can show you how to deal with the merge conflict next time we meet (next Friday), or you can pick a time tomorrow or earlier next week: https://calendar.app.google/eCWobQteVLViq5NJ8
It'll be an quick fix, just something easier to show over Zoom than via email.

@Aariq Aariq linked an issue Jul 26, 2023 that may be closed by this pull request
@@ -86,6 +86,7 @@ az_daily <- function(station_id = NULL, start_date = NULL, end_date = NULL) {
)) %>%
dplyr::filter(.data$meta_station_id != "az99") %>%
dplyr::mutate(datetime = lubridate::ymd(.data$datetime)) %>%
dplyr::mutate(wind_2min_timestamp = lubridate::with_tz(lubridate::parse_date_time(.data$wind_2min_timestamp, orders = "ymdHMSz"), tzone = "America/Phoenix")) %>%
#convert NAs
dplyr::mutate(
dplyr::across(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just below this is code to change values like -9999 to NA, but it currently skips wind_2min_timestamp. I think all you need to do is:

...
dplyrr::across(
  c(wind_2min_timestamp, tidyselect(where(is.numeric))), 
  function(x)
  ...

Copy link
Member Author

@jeremylweiss jeremylweiss Jul 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without adding the above code to include wind_2min_timestamp in the conversion of NAs, existing code with wind_2min_timestamp as character automatically converts corresponding nodata values of -99999 to NA while throwing a warning 27 failed to parse, as with

az_daily(start_date = "2023-03-22", end_date = "2023-03-22")

Only one of the 28 stations at that point had wind_2min_timestamp daily values on that date.

I think the nodata value conversion needs to happen somehow in conjunction with character-to-datetime conversion of this variable.

Copy link
Member Author

@jeremylweiss jeremylweiss Jul 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the above code has trouble with wind_2min_timestamp, as it is an object of class "c('POSIXct', 'POSIXt')". This is after the character-to-datetime conversion

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aariq please review solutions in az_daily() and az_hourly() I just pushed on this branch. I think I figured it out

@Aariq Aariq linked an issue Jul 28, 2023 that may be closed by this pull request
Copy link
Member

@Aariq Aariq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Other than an unnecessary (but perfectly functional) across(), this looks great to me.

R/az_daily.R Outdated Show resolved Hide resolved
R/az_daily.R Outdated Show resolved Hide resolved
) %>%
dplyr::mutate(
wind_2min_timestamp = lubridate::with_tz(
lubridate::parse_date_time(.data$wind_2min_timestamp, orders = "ymdHMSz"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahhh, I understand this now. So pars_date_time does actually understand the timezone, it just converts it automatically to UTC and with_tz() converts it back to MST. Cool!

dplyr::mutate(date_datetime = lubridate::ymd_hms(.data$date_datetime)) %>%
dplyr::mutate(
date_datetime =
lubridate::force_tz(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but here, ymd_hms() doesn't see a timezone and you need force_tz() to keep the time and only change the timezone. Nice!

@jeremylweiss jeremylweiss merged commit 6eb7332 into main Aug 4, 2023
6 checks passed
@jeremylweiss jeremylweiss deleted the 2min-wind-timestamp-variable-type branch August 4, 2023 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle new wind variables in API
2 participants